<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 订单号 </el-col>
            <el-col :span="16">
              <el-input v-model="ser.order_number" type="number"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 客户 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.customer_id"
                filterable
                placeholder="请选择客户"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in CustomerList"
                  :key="index"
                  :label="item.customer_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 产品 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.product_id"
                filterable
                @change="product_id_change"
                placeholder="请选择产品"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in productList"
                  :key="index"
                  :label="item.product_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 型号 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.model_number_id"
                placeholder="请选择型号"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in ModelNumberList"
                  :key="index"
                  :label="item.model_number"
                  :value="item.pivot.model_number_id"
                >
                </el-option>
              </el-select>
              
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 订单日期范围 </el-col>
            <el-col :span="16">
              <el-date-picker
                v-model="order_data"
                @change="order_data_change"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-button @click="search(0)" class="add_btn" icon="el-icon-search"
            >查询</el-button>
          <el-button @click="searchreset" icon="el-icon-refresh" class="add_btn"
            >重置</el-button>
          <el-button @click="search(2)" icon="el-icon-plus" class="add_btn"
            >导出</el-button
          >
          
        </el-col>
      </el-row>
    </div>

    <el-table :data="dataList" style="width: 100%" border>
      <!-- <el-table-column type="selection"> </el-table-column> -->
      <el-table-column prop="created_at" label="入库日期" width="160">
      </el-table-column>
      <el-table-column label="客户名称" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.customer.customer_name }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.order_number }}
        </template>
      </el-table-column>
      <el-table-column label="产品名称" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.product_name.product_name }}
        </template>
      </el-table-column>
      <el-table-column label="产品型号" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.model_number_name.model_number }}
        </template>
      </el-table-column>
      <el-table-column label="方向" width="120">
        <template slot-scope="scope">
          {{ scope.row.direction }}
        </template>
      </el-table-column>
      <el-table-column label="规格" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.specification.specification }}
        </template>
      </el-table-column>
      <el-table-column label="颜色" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.color.color }}
        </template>
      </el-table-column>
      <el-table-column label="生产批号" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.batch_number }}
        </template>
      </el-table-column>
      <el-table-column label="备注" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.remark }}
        </template>
      </el-table-column>
      <el-table-column prop="logistics_status" label="发货状态" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.logistics_status == 1">待发货</span>
          <span v-if="scope.row.logistics_status == 2">未发货</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
            <el-button  icon="el-icon-sort" @click="beihuos(scope.row)">
              转到成品库</el-button
            >
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>


    <el-dialog
      title="选择客户"
      :visible.sync="isbeihuo"
      width="30%"
      :close-on-click-modal="false"
    >
               <el-select
                  v-model="beihuolist.customer_id"
                filterable
                placeholder="请选择客户"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in CustomerList"
                  :key="index"
                  :label="item.customer_name"
                  :value="item.id"
                >
              </el-option>
            </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isset = false">取消</el-button>
        <el-button @click="push_sets">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { 
  getStockUpList,setStockUpStatus,
  getCustomerList,
  getModelNumberList,
  getProductList
 } from "@/api/product/index";
export default {
  data() {
    return {
      isbeihuo:false,
       // 搜索
      
       ModelNumberLists: [],
      ModelNumberList: [],
      order_data: [],
      ser: {
        model_number_id: "",
        order_number: "",
        customer_id: "",
        product_id: "",
        order_date: "",
        is_replenishment: "",
        is_stock_up: "",
        start_time: "",
        end_time: "",
      },
      CustomerList: [],
      productList: [],

      dataList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      beihuolist:{
        id:'',
        order_id:'',
        is_stock_up:2,
        customer_id:''
      }
    };
  },
  mounted() {
    this.getlist();

    this.getproduct();
    this.getModel();
    this.getCustomer();

  },
  methods: {
     // 指定查询
     search(type) {
      if(type==0){
        this.pagination.per_page=10
        this.pagination.page=1
      }
      getStockUpList({...this.ser,...this.pagination}).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
          if (type == 2) {
            if (res.data.data.length > 0) {
              this.$message.success("正在导出订单列表");
              this.exports_w();
            }
          }
        }
      });
    },
    // 导出
    exports_w() {
      let data = {
        ...this.ser,
        export: 1,
      };
      downloadcode(data).then((res) => {
        //创建一个隐藏的a连接，
        const link = document.createElement("a");
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        link.style.display = "none";
        //设置连接
        link.href = URL.createObjectURL(blob);
        link.download = "订单信息";
        document.body.appendChild(link);
        //模拟点击事件
        link.click();
      });
    },
    searchreset() {
      this.ser = {
        model_number_id: "",
        order_number: "",
        customer_id: "",
        product_id: "",
        order_date: "",
        is_replenishment: "",
        is_stock_up: "",
        start_time: "",
        end_time: ""
      };
      this.getlist()
    },
    //客户列表
    getCustomer() {
      getCustomerList().then((res) => {
        if (res.code == 200) {
          this.CustomerList = res.data.data;
        }
      });
    },
    //产品列表
    getproduct() {
      getProductList().then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
        }
      });
    },
    // 型号列表
    getModel() {
      getModelNumberList().then((res) => {
        if (res.code == 200) {
          this.ModelNumberLists = res.data.data;
        }
      });
    },
    order_data_change(val) {
      this.ser.start_time = val[0];
      this.ser.end_time = val[1];
    },
    product_id_change(val) {
      console.log(val);
      this.ModelNumberList = [];
      this.productList.forEach((item) => {
        if (item.id == val) {
          console.log(item);
          // 型号列表
          this.ModelNumberList = item.model_number;
        }
      });

    },
    beihuos(row){
      this.isbeihuo=true;
      this.beihuolist.id= row.id
      this.beihuolist.order_id= row.order_id
    },

    push_sets() {
      if(this.beihuolist.customer_id==''){
          this.$message.error('请选择客户')
          return false
      }
      setStockUpStatus(this.beihuolist).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
          this.getlist();
        }
      });
    },
    // 获取成品库列表
    getlist() {
      getStockUpList({...this.pagination,...this.ser}).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    handleSizeChange(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
  },
};
</script>

<style scoped>
#printArea {
  width: 100%;
  margin: 0 auto;
}

#printArea > div {
  margin-top: 50px;
}
#printArea > div:first-child {
  margin-top: 0px;
}
#printArea table {
  width: 100%;
  border-collapse: collapse;
}
#printArea table th {
  color: white;
  height: 50px;
  line-height: 50px;
  font-size: 35px;
}
#printArea table {
  border: 1px solid #c5c5c5;
}
#printArea table td,
table th {
  border: 1px solid #c5c5c5;
  text-align: center;
  padding: 0;
  margin: 0;
}

#printArea table tr td {
  height: 35px;
  line-height: 35px;
}
#printArea table thead th {
  background-color: #f4615c;
}

#printArea table tr th,
table tr td {
  padding: 5px 12px;
}

@page {
  size: auto; /* auto is the initial value */
  margin: 3mm; /* this affects the margin in the printer settings */
}
.procedure {
  word-wrap: break-word;
}
</style>
